package ops.view;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import ops.WindowManager;
import ops.controller.ControllerManager;
import ops.controller.CourseController;
import ops.i18n.Resources;
import ops.util.ViewHelper;
import ops.view.tables.Column;
import ops.view.tables.TableMaker;

public class CourseSubjectsDialog extends JDialog {

	private TableMaker currentSubjectsTable;
	private TableMaker availableSubjectsTable;
	
	private JButton addSubjectButton;
	private JButton removeSubjectButton;
	
	public CourseSubjectsDialog() {
		super(WindowManager.getInstance().getApplicationFrame(), 
				Resources.getInstance().getString("course.subjects.edit"), true);
		
		setLayout(new BorderLayout());
		setSize(600, 400);
		
		WindowManager.getInstance().centerWindow(this);
		
		init();
	}

	private void init() {
		initButtons();
		initTables();
	}

	private void initButtons() {
		JPanel buttonsPanel = new JPanel(new BorderLayout());
	
		ButtonListener listener = new ButtonListener();
		
		addSubjectButton = ViewHelper.makeAddButton();
		addSubjectButton.addActionListener(listener);
		removeSubjectButton = ViewHelper.makeRemoveButton();
		removeSubjectButton.addActionListener(listener);
		buttonsPanel.add(addSubjectButton, BorderLayout.WEST);
		buttonsPanel.add(removeSubjectButton, BorderLayout.EAST);

		add(buttonsPanel, BorderLayout.SOUTH);		
	}

	private void initTables() {
		Resources resources = Resources.getInstance();

		JPanel tablePanel = new JPanel(new GridLayout(1, 2, 10, 10));
		
		availableSubjectsTable = new TableMaker();
		availableSubjectsTable.addColumn(new Column(resources.getString("subject.code"), Integer.class, 5));
		availableSubjectsTable.addColumn(new Column(resources.getString("subject.name"), String.class, 30));
		availableSubjectsTable.setControlButtons(addSubjectButton);
		
		JScrollPane availableScrollPane = ViewHelper.makeScrollPane(availableSubjectsTable.getTable());
		availableScrollPane.setBorder(BorderFactory.createTitledBorder(resources.getString("course.subjects.available")));
		tablePanel.add(availableScrollPane);

		currentSubjectsTable = new TableMaker();
		currentSubjectsTable.addColumn(new Column(resources.getString("subject.code"), Integer.class, 5));
		currentSubjectsTable.addColumn(new Column(resources.getString("subject.name"), String.class, 30));
		currentSubjectsTable.setControlButtons(removeSubjectButton);
		
		JScrollPane currentScrollPane = ViewHelper.makeScrollPane(currentSubjectsTable.getTable());
		currentScrollPane.setBorder(BorderFactory.createTitledBorder(resources.getString("course.subjects.current")));
		tablePanel.add(currentScrollPane);		
		
		add(tablePanel, BorderLayout.CENTER);
	}
	
	public void addCurrent(Object... data) {
		currentSubjectsTable.addRow(data);
	}
	
	public void clearCurrent() {
		currentSubjectsTable.clearTable();
	}
	
	public void addAvailable(Object... data) {
		availableSubjectsTable.addRow(data);
	}	
	
	public void clearAvailable() {
		availableSubjectsTable.clearTable();
	}
	
	private class ButtonListener implements ActionListener {

		public void actionPerformed(ActionEvent e) {
			CourseController courseController = ControllerManager.getInstance().getCourseController();
			
			JButton button = (JButton) e.getSource();
			
			if (!button.isEnabled()) {
				return;
			}
			
			int subjectCode = 0;
			
			if (button == addSubjectButton) {
				subjectCode = (Integer) availableSubjectsTable.getValueAt(availableSubjectsTable.getSeletedRow(), 0);
				courseController.addSubjectToCourse(subjectCode);
			} else if (button == removeSubjectButton) {
				subjectCode = (Integer) currentSubjectsTable.getValueAt(currentSubjectsTable.getSeletedRow(), 0);
				courseController.removeSubjectCourse(subjectCode);
			}
		}
		
	}
}
